﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VectorRacer
{
    /// <summary>
    /// Interaction logic for PlayerLayer.xaml
    /// </summary>
    public partial class PlayerLayer : UserControl
    {
        public static readonly DependencyProperty CheckSizeProperty = DependencyProperty.Register("CheckSize", typeof(int), typeof(PlayerLayer));

        private Player _player;

        public PlayerLayer()
        {
            InitializeComponent();
            this.CheckSize = 20;

            this.Background = Brushes.Transparent;
        }

        public PlayerLayer(Player player, Client gameSession)
            : this()
        {
            _player = player;
            _player.Moved += new EventHandler(_player_Moved);
            this.DataContext = gameSession;
            UpdatePredictionBox();
            DrawCurrentPosition();
        }

        protected void DrawCurrentPosition()
        {
            _pathFigure.Segments.Add(new LineSegment(new Point(_player.Position.X * CheckSize, _player.Position.Y * CheckSize), true));
        }

        protected void UpdatePredictionBox()
        {
            Canvas.SetLeft(_nextMovePreview, (_player.ResultantPosition.X - 1) * CheckSize);
            Canvas.SetTop(_nextMovePreview, (_player.ResultantPosition.Y - 1) * CheckSize);

            _nextMove11.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X - 1, _player.ResultantPosition.Y - 1)) == ChechMoveResult.Correct;
            _nextMove12.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X, _player.ResultantPosition.Y - 1)) == ChechMoveResult.Correct;
            _nextMove13.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X + 1, _player.ResultantPosition.Y - 1)) == ChechMoveResult.Correct;
            _nextMove21.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X - 1, _player.ResultantPosition.Y)) == ChechMoveResult.Correct;
            _nextMove22.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X, _player.ResultantPosition.Y)) == ChechMoveResult.Correct;
            _nextMove23.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X + 1, _player.ResultantPosition.Y)) == ChechMoveResult.Correct;
            _nextMove31.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X - 1, _player.ResultantPosition.Y + 1)) == ChechMoveResult.Correct;
            _nextMove32.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X, _player.ResultantPosition.Y + 1)) == ChechMoveResult.Correct;
            _nextMove33.IsEnabled = GameSession.CheckMove(_player, new Coordinates(_player.ResultantPosition.X + 1, _player.ResultantPosition.Y + 1)) == ChechMoveResult.Correct;
        }

        void _player_Moved(object sender, EventArgs e)
        {
            DrawCurrentPosition();
            UpdatePredictionBox();
        }

        public int CheckSize
        {
            get { return (int)this.GetValue(CheckSizeProperty); }
            set { this.SetValue(CheckSizeProperty, value); }
        }

        protected Client GameSession
        {
            get { return (Client)DataContext; }
        }
    }
}
